home *** CD-ROM | disk | FTP | other *** search
Text File | 1999-01-08 | 8.1 KB | 332 lines | [TEXT/CWIE] |
- ///--------------------------------------------------------------------------------------
- // Level.c
- //
- // Created 10/7/96
- ///--------------------------------------------------------------------------------------
-
-
- #include <SWIncludes.h>
- #include <SWGameUtils.h>
-
- #include "Level.h"
- #include "SWSounds.h"
- #include "NewSprite.h"
- #include "Shark Attack.h"
- #include "SpriteMoveProcs.h"
- #include "Special Effects.h"
- #include "GlobalVariables.h"
-
- #define kGetReadyTime 60 // How many frames to display "Get Ready" message
-
- #define kMaxNumFishOnScreen 4 // Max number of fish and sharks that
- #define kMaxNumSharksOnScreen 1 // can be on the screen at once.
- #define kFishSpeed 3
- #define kSharkSpeed 4
-
-
- // Stores whether the last fish was added on the left side of the screen or not.
- Boolean gAddedOnLeftSide;
-
-
- ///--------------------------------------------------------------------------------------
- // DoGetReadyMessage
- ///--------------------------------------------------------------------------------------
-
- void DoGetReadyMessage( void )
- {
- unsigned long junkTime;
-
- SWSetPortToWorkArea(gSpriteWorldP);
-
- ForeColor(blackColor);
- PaintRect(&gSpriteWorldP->backRect);
-
- ForeColor(redColor);
- DrawTextInWorkArea("\pGet Ready!", 48);
-
- WipeWindow(gSpriteWorldP);
- Delay(90, &junkTime);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // DoNextLevelMessage
- ///--------------------------------------------------------------------------------------
-
- void DoNextLevelMessage( void )
- {
- unsigned long junkTime;
- Str255 levelString;
-
- SWSetPortToWorkArea(gSpriteWorldP);
-
- ForeColor(blackColor);
- PaintRect(&gSpriteWorldP->backRect);
-
- ForeColor(cyanColor);
- DrawTextInWorkArea("\pLevel ", 48);
- NumToString(gCurrentLevel, levelString);
- ForeColor(cyanColor);
- DrawString(levelString);
- ForeColor(blackColor);
-
- WipeWindow(gSpriteWorldP);
- Delay(120, &junkTime);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // DoGameOver
- ///--------------------------------------------------------------------------------------
-
- void DoGameOver( void )
- {
- unsigned long junkTime;
-
- SWSetPortToWorkArea(gSpriteWorldP);
-
- ForeColor(blackColor);
- PaintRect(&gSpriteWorldP->backRect);
-
- ForeColor(blueColor);
- DrawTextInWorkArea("\pGame Over", 64);
-
- WipeWindow(gSpriteWorldP);
- PlaySound(kSharkDeadSnd, 2, kFindEmptyChannel);
- Delay(120, &junkTime);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // DrawTextInWorkArea
- ///--------------------------------------------------------------------------------------
-
- void DrawTextInWorkArea(StringPtr textString, short fontSize)
- {
- Rect textRect;
-
- SWSetPortToWorkArea(gSpriteWorldP);
-
- TextFont(systemFont);
- TextFace(bold);
- TextSize(fontSize);
-
- textRect.top = 0;
- textRect.left = 0;
- textRect.bottom = 0;
- textRect.right = StringWidth(textString);
- CenterRect(&textRect, &gSpriteWorldP->backRect);
-
- MoveTo(textRect.left, textRect.top);
- DrawString(textString);
-
- ForeColor(blackColor);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // SetUpLevel - prepare the animation
- ///--------------------------------------------------------------------------------------
-
- void SetUpLevel( void )
- {
- gFishDelay = 0;
- gSharkDelay = kMinNewSharkDelay;
-
- AddSubmarine();
- }
-
-
- ///--------------------------------------------------------------------------------------
- // RestartLevel - called when the submarine dies
- ///--------------------------------------------------------------------------------------
-
- void RestartLevel( void )
- {
- RemoveAllSprites(gSpriteWorldP);
-
- DoGetReadyMessage();
-
- SetUpLevel();
- SWUpdateSpriteWorld(gSpriteWorldP, false);
- WipeWindow(gSpriteWorldP);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AdvanceLevel - called when it's time to go on to the next level
- ///--------------------------------------------------------------------------------------
-
- void AdvanceLevel( void )
- {
- short numFrames = 90;
-
- // Run animation for a little bit before stopping
- while ( numFrames )
- {
- SWProcessSpriteWorld(gSpriteWorldP);
- SWAnimateSpriteWorld(gSpriteWorldP);
- UpdateKeys();
-
- if (gSpriteWorldP->frameHasOccurred)
- numFrames--;
- }
-
- RemoveAllSprites(gSpriteWorldP);
-
- gCurrentLevel++;
- gNextLevelScore += kAdvanceLevelScore; // Set the score for the next level
- DoNextLevelMessage();
-
- SetUpLevel();
- SWUpdateSpriteWorld(gSpriteWorldP, false);
- WipeWindow(gSpriteWorldP);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // ProcessLevel - keep adding fish to the level
- ///--------------------------------------------------------------------------------------
-
- void ProcessLevel( void )
- {
- // Don't add any fish if it's time to advance to the next level
- if (gScore > gNextLevelScore)
- return;
-
- if (gNumFishOnScreen < kMaxNumFishOnScreen)
- {
- if (gFishDelay > 0)
- {
- gFishDelay--;
- }
- else if (GetRandom(0, kFishRandomness) == 0)
- {
- AddFish();
- gFishDelay = kMinNewFishDelay;
- }
- }
-
-
- if (gNumSharksOnScreen < kMaxNumSharksOnScreen)
- {
- if (gSharkDelay > 0)
- {
- gSharkDelay--;
- }
- else if (GetRandom(0, kSharkRandomness) == 0)
- {
-
- AddShark();
- gSharkDelay = kMinNewSharkDelay;
- }
- }
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AddSubmarine
- ///--------------------------------------------------------------------------------------
-
- void AddSubmarine( void )
- {
- gSubCloneP = NewSubSprite();
-
- SWSetSpriteLocation(gSubCloneP, ((SubStructPtr)gSubCloneP)->horizPos,
- ((SubStructPtr)gSubCloneP)->vertPos);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AddFish - add a fish to the animation
- ///--------------------------------------------------------------------------------------
-
- void AddFish( void )
- {
- short horizLoc, vertLoc;
- short addToLeftSide;
- short horizDelta;
- SpritePtr fishSpriteP;
-
- fishSpriteP = NewFishSprite();
-
- // Make the next fish come from the opposite side of the screen most of the time.
- if (gAddedOnLeftSide)
- {
- if (GetRandom(0,30) == 0)
- addToLeftSide = true;
- else
- addToLeftSide = false;
- }
- else
- {
- if (GetRandom(0,30) == 0)
- addToLeftSide = false;
- else
- addToLeftSide = true;
- }
-
- if (addToLeftSide)
- {
- // Add fish to left size
- horizDelta = kFishSpeed;
- horizLoc = -(fishSpriteP->destFrameRect.right - fishSpriteP->destFrameRect.left);
- SWSetSpriteFrameRange(fishSpriteP, 0, 2);
- SWSetCurrentFrameIndex(fishSpriteP, 0);
- gAddedOnLeftSide = true;
- }
- else
- {
- // Add fish to right side
- horizDelta = -kFishSpeed;
- horizLoc = gSpriteWorldP->backRect.right;
- SWSetSpriteFrameRange(fishSpriteP, 3, 5);
- SWSetCurrentFrameIndex(fishSpriteP, 3);
- gAddedOnLeftSide = false;
- }
-
- vertLoc = GetRandom(0, gSpriteWorldP->backRect.bottom -
- (fishSpriteP->destFrameRect.bottom - fishSpriteP->destFrameRect.top));
-
- SWSetSpriteMoveDelta(fishSpriteP, horizDelta, 0);
- SWSetSpriteLocation(fishSpriteP, horizLoc, vertLoc);
- }
-
-
- ///--------------------------------------------------------------------------------------
- // AddShark - add a shark to the animation
- ///--------------------------------------------------------------------------------------
-
- void AddShark( void )
- {
- short horizPos, vertPos;
- SpritePtr sharkSpriteP;
- SharkStructPtr sharkStructP;
-
- sharkSpriteP = NewSharkSprite();
- sharkStructP = (SharkStructPtr)sharkSpriteP;
-
- // Add shark on right or left side
- if (GetRandom(0,1) == 0)
- {
- sharkStructP->horizDelta = -kSharkSpeed;
- SWSetSpriteFrameRange(sharkSpriteP, 3, 5);
- SWSetCurrentFrameIndex(sharkSpriteP, 3);
- horizPos = gSpriteWorldP->backRect.right;
- }
- else
- {
- sharkStructP->horizDelta = kSharkSpeed;
- SWSetSpriteFrameRange(sharkSpriteP, 0, 2);
- SWSetCurrentFrameIndex(sharkSpriteP, 0);
- horizPos = -(sharkSpriteP->destFrameRect.right - sharkSpriteP->destFrameRect.left);
- }
-
- // Place shark at same vertical location as sub
- vertPos = gSubCloneP->destFrameRect.top + kSubHeight/2 - kSharkHeight/2;
-
- sharkStructP->horizPos = horizPos;
- sharkStructP->vertPos = vertPos;
- SWSetSpriteLocation(sharkSpriteP, horizPos, vertPos);
- }
-